package floappcontest.util.converters;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.model.SelectItem;

import floappcontest.core.beans.Tutor;


public class TutorConverter implements Converter {

	private final List<SelectItem> list;


	public TutorConverter( List<SelectItem> list ) {
		this.list = list;
	}


	public Tutor getAsObject( @SuppressWarnings( "unused" )
	FacesContext context, @SuppressWarnings( "unused" )
	UIComponent uicomponent, String value ) throws ConverterException {
		for ( SelectItem item : getList() ) {
			if ( item.getLabel().equalsIgnoreCase( value ) ) {
				return (Tutor) item.getValue();
			}
		}
		return null;
	}


	public String getAsString( @SuppressWarnings( "unused" )
	FacesContext context, @SuppressWarnings( "unused" )
	UIComponent uicomponent, Object value ) throws ConverterException {
		return ((Tutor) value).getLogin();
	}


	public List<SelectItem> getList() {
		return this.list;
	}
}